package pers.goetboy.common.vaild;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * 手机号 校验类
 *
 * @author goetb
 * @see IsEnum
 */
public class PhoneValidator implements ConstraintValidator<Phone, Object> {
    private String regex;


    @Override
    public void initialize(Phone constraintAnnotation) {
        regex = constraintAnnotation.regex();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        return Pattern.compile(regex).matcher(value.toString()).matches();
    }
}
